﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class ListOfCategoryLevel1 : System.Web.UI.Page
    {
        Dictionary<int, int> listMostPopular = null;
        CDanhMuc _cdm = new CDanhMuc();
        CProduct _csp = new CProduct();

        protected void Page_Load(object sender, EventArgs e)
        {
            int pid = 0;
            int.TryParse(Request.Params["pid"].ToString(), out pid);
            if (pid > 0)
            {
                LoadCategories(pid);
                LoadMostPopular();
                LoadLatestProduct(pid);
                LoadProducts();
            }
        }

        private void LoadCategories(int pid)
        {
            DanhMuc dm = _cdm.GetByID(pid);
            if (dm != null)
            {
                listMostPopular = new Dictionary<int, int>();
                lbLinkHeader.Text = dm.TenDanhMuc;
                lbCategoryName.Text = dm.TenDanhMuc;
                lbNameMostPopular.Text = dm.TenDanhMuc;
                lbNameProduct.Text = dm.TenDanhMuc;
                gvCategories.DataSource = _cdm.GetChildrenOfCategory(pid, 1);
                gvCategories.DataBind();
            }
        }

        private void LoadMostPopular()
        {
            if (listMostPopular.Count > 0)
            {
                DanhMuc dm = null;
                var result = listMostPopular.OrderByDescending(u => u.Value).ToList();
                int n = Math.Min(listMostPopular.Count, 4);
                for (int i = 0; i < n; i++)
                {
                    int key = result[i].Key;
                    int value = result[i].Value;
                    dm = _cdm.GetByID(key);
                    string strImage = "";
                    switch (i + 1)
                    {
                        case 1:
                            hlMostPopular1.Text = dm.TenDanhMuc;
                            hlMostPopular1.NavigateUrl = "ListProductOfCategory.aspx?pid=" + dm.IDDanhMuc;
                            dlMostPopular1.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlMostPopular1.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgMostPopular1.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 2:
                            hlMostPopular2.Text = dm.TenDanhMuc;
                            hlMostPopular2.NavigateUrl = "ListProductOfCategory.aspx?pid=" + dm.IDDanhMuc;
                            dlMostPopular2.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlMostPopular2.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgMostPopular2.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 3:
                            hlMostPopular3.Text = dm.TenDanhMuc;
                            hlMostPopular3.NavigateUrl = "ListProductOfCategory.aspx?pid=" + dm.IDDanhMuc;
                            dlMostPopular3.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlMostPopular3.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgMostPopular3.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 4:
                            hlMostPopular4.Text = dm.TenDanhMuc;
                            hlMostPopular4.NavigateUrl = "ListProductOfCategory.aspx?pid=" + dm.IDDanhMuc;
                            dlMostPopular4.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlMostPopular4.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgMostPopular4.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                //khong co most popular
            }
        }

        private void LoadLatestProduct(int id)
        {
            List<SanPham> list = _csp.GetTopTenLatestOfCategory(id);
            dlLatestProducts.DataSource = list;
            dlLatestProducts.DataBind();
        }

        private void LoadProducts()
        {
            if (listMostPopular.Count > 0)
            {
                DanhMuc dm = null;
                var result = listMostPopular.OrderByDescending(u => u.Value).ToList();
                int n = Math.Min(listMostPopular.Count, 4);
                for (int i = 0; i < n; i++)
                {
                    int key = result[i].Key;
                    dm = _cdm.GetByID(key);
                    string strImage = "";
                    switch (i + 1)
                    {
                        case 1:
                            lbProduct1.Text = dm.TenDanhMuc;
                            dlProduct1.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlProduct1.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgProduct1.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 2:
                            lbProduct2.Text = dm.TenDanhMuc;
                            dlProduct2.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlProduct2.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgProduct2.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 3:
                            lbProduct3.Text = dm.TenDanhMuc;
                            dlProduct3.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlProduct3.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgProduct3.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        case 4:
                            lbProduct4.Text = dm.TenDanhMuc;
                            dlProduct4.DataSource = _cdm.GetChildrenOfCategory(dm.IDDanhMuc, 1, 5);
                            dlProduct4.DataBind();
                            foreach (SanPham sp in dm.SanPhams)
                            {
                                if (sp.HinhAnh != null && !sp.HinhAnh.Equals(""))
                                {
                                    strImage = sp.HinhAnh;
                                    break;
                                }
                            }
                            if (strImage.Equals(""))//Neu trong co thi tim trong cac danh muc con
                            {
                                strImage = _cdm.GetImageOfCateogry(dm.IDDanhMuc);
                            }
                            if (!strImage.Equals(""))//co hinh anh
                            {
                                imgProduct4.ImageUrl = "~/Images/Product/" + strImage;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                //khong co most popular
            }
        }

        protected void gvCategories_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int ID = 0;
                ID = (int)DataBinder.Eval(e.Row.DataItem, "IDDanhMuc");
                if (ID > 0)
                {
                    GridView gvItem1 = (GridView)e.Row.FindControl("gvItem1");
                    Label lbQuantity = (Label)e.Row.FindControl("lbQuantity1");
                    if (gvItem1 != null && lbQuantity!=null)
                    {
                        int quantity = (_cdm.GetQuantityOfCategoryProduct(ID) + _cdm.GetByID(ID).SanPhams.Count);
                        if (quantity > 0)
                        {
                            lbQuantity.Text = "[" + quantity + " Products]";
                            listMostPopular.Add(ID, quantity);
                        }
                        gvItem1.DataSource = _cdm.GetChildrenOfCategory(ID, 1, 5);
                        gvItem1.DataBind();
                        if (_cdm.CheckCategoryChildrenMoreThan5(ID))
                        {
                            GridView gvItemMore = (GridView)e.Row.FindControl("gvItem1More");
                            Panel pnMoreLess = (Panel)e.Row.FindControl("pnMoreLess1");
                            if (gvItemMore != null && pnMoreLess != null)
                            {
                                pnMoreLess.Visible = true;
                                gvItemMore.DataSource = _cdm.GetChildrenOfCategory(ID, 6);
                                gvItemMore.DataBind();
                            }
                        }

                    }
                }
            }
        }

        public string CheckHinhAnh(object obj)
        {
            string strHinh = "";
            if (obj != null)
            {
                strHinh = "~/Images/Product/" + obj.ToString();
            }
            return strHinh;
        }
    }
}